/**
 * 路由拦截模块
 */

export default {
	onLaunch() {
		// 初始化路由拦截
		this.routerInterceptor();
	},
	methods: {
		/**
		 * 路由拦截器
		 */
		routerInterceptor() {
			// 登录页
			const loginPage = "/pages/tabBar/login/login";
			// 白名单【需手动配置】
			const whiteList = [
				loginPage,
				"/pages/tabBar/qidong/qidong",
				"/pages/guidance/guidance",
				"/pages/tabBar/login/login",
				"/pages/dlzc/agreement/agreement",
				"/pages/dlzc/bandMobile/bandMobile",
				"/pages/dlzc/codeLogin/codeLogin",
				"/pages/dlzc/forgetPwd/forgetPwd",
				"/pages/tabBar/passwordLogin/passwordLogin",
				"/pages/dlzc/privacyPolicy/privacyPolicy",
				"/pages/dlzc/register/register",
				"/pages/tabBar/register/register",
			];

			/*
				注意:switchTab监听,小程序端无效[因为小程序端调的原生方法,所以无效]
			*/
			// console.debug('nav interceptor start');
			const navInvokeHandler = (res, type) => {
				if (!uni.getStorageSync('USER_TOKEN')) {
					var valid = whiteList.some(t => res.url.indexOf(t) != -1);
					if (!valid) {
						res.url = '';
						uni.navigateTo({
							url: loginPage
						});
					}
				}
			}
			uni.addInterceptor('navigateTo', {
				invoke: res => navInvokeHandler(res, 'navigateTo')
			});
			uni.addInterceptor('redirectTo', {
				invoke: res => navInvokeHandler(res, 'redirectTo')
			});
			uni.addInterceptor('switchTab', {
				invoke: res => navInvokeHandler(res, 'switchTab')
			});
			uni.addInterceptor('reLaunch', {
				invoke: res => navInvokeHandler(res, 'reLaunch')
			});
		}
	}
}